﻿using System;
using System.Collections.Generic;
using SteeringBehaviors;
using UnityEngine;

// Token: 0x020001CD RID: 461
public class MovementCharacter : MonoBehaviour
{
	// Token: 0x17000150 RID: 336
	// (get) Token: 0x060009D0 RID: 2512 RVA: 0x00058B24 File Offset: 0x00056D24
	public float MaxSpeed
	{
		get
		{
			return this.maxSpeedBase * this.scale * this.speedModifier;
		}
	}

	// Token: 0x17000151 RID: 337
	// (get) Token: 0x060009D1 RID: 2513 RVA: 0x00058B3A File Offset: 0x00056D3A
	public float MaxAccel
	{
		get
		{
			return this.maxAccel * this.scale * this.speedModifier;
		}
	}

	// Token: 0x17000152 RID: 338
	// (get) Token: 0x060009D2 RID: 2514 RVA: 0x00058B50 File Offset: 0x00056D50
	public float MaxRotation
	{
		get
		{
			return this.maxRotation * this.speedModifier;
		}
	}

	// Token: 0x17000153 RID: 339
	// (get) Token: 0x060009D3 RID: 2515 RVA: 0x00058B5F File Offset: 0x00056D5F
	public float MaxAngularAccel
	{
		get
		{
			return this.maxAngularAccel * this.speedModifier;
		}
	}

	// Token: 0x17000154 RID: 340
	// (get) Token: 0x060009D4 RID: 2516 RVA: 0x00058B70 File Offset: 0x00056D70
	public float orientation
	{
		get
		{
			float num = this.rbody.rotation.eulerAngles.y;
			if (num < 0f)
			{
				num += 360f;
			}
			else if (num > 360f)
			{
				num -= 360f;
			}
			return num;
		}
	}

	// Token: 0x17000155 RID: 341
	// (get) Token: 0x060009D5 RID: 2517 RVA: 0x00058BC5 File Offset: 0x00056DC5
	public float scale
	{
		get
		{
			return this.entity.AccurateScale;
		}
	}

	// Token: 0x060009D6 RID: 2518 RVA: 0x00058BD4 File Offset: 0x00056DD4
	private void Awake()
	{
		this.walkController = base.gameObject.AddComponent<WalkController>();
		this.velocity = Vector3.zero;
		this.steering = new SteeringOutput();
		this.steeringBehaviors = new List<SteerBehavior>();
		this.obstacleDetector = base.GetComponent<ObstacleDetector>();
		this.anim = base.GetComponent<AnimationManager>();
		this.entity = base.GetComponent<EntityBase>();
		this.myTransform = base.transform;
		this.upVector = Vector3.up;
	}

	// Token: 0x060009D7 RID: 2519 RVA: 0x00058C4E File Offset: 0x00056E4E
	private void Start()
	{
		this.rbody = base.GetComponent<Rigidbody>();
	}

	// Token: 0x060009D8 RID: 2520 RVA: 0x00058C5C File Offset: 0x00056E5C
	private void FixedUpdate()
	{
		if (!this.move || this.behaviorCount == 0)
		{
			return;
		}
		this.forward = ((!this.overrideDirection) ? this.myTransform.forward : this.overrideVector);
		if (this.rotation != 0f)
		{
			float num = this.orientation;
			num += this.rotation * Time.deltaTime;
			this.rbody.rotation = Quaternion.AngleAxis(num, this.upVector);
		}
		float num2 = this.direction.z * Time.deltaTime;
		if (this.overrideDirection)
		{
			num2 = Mathf.Abs(num2);
		}
		if (num2 != 0f)
		{
			Vector3 position = this.forward;
			position.x *= num2;
			position.y *= num2;
			position.z *= num2;
			Vector3 position2 = this.rbody.position;
			position.x += position2.x;
			position.y += position2.y;
			position.z += position2.z;
			this.rbody.position = position;
		}
		if (this.stop)
		{
			this.Stop();
		}
	}

	// Token: 0x060009D9 RID: 2521 RVA: 0x00058DAF File Offset: 0x00056FAF
	private void LateUpdate()
	{
		this.behaviorCount = this.steeringBehaviors.Count;
		if (!this.move || this.behaviorCount == 0)
		{
			return;
		}
		this.UpdateMovementVariables();
	}

	// Token: 0x060009DA RID: 2522 RVA: 0x00058DE0 File Offset: 0x00056FE0
	private void UpdateMovementVariables()
	{
		float deltaTime = Time.deltaTime;
		float num = this.MaxSpeed / this.currentSpeedDivider;
		this.steering.linear.x = 0f;
		this.steering.linear.y = 0f;
		this.steering.linear.z = 0f;
		this.steering.angular = 0f;
		foreach (SteerBehavior steerBehavior in this.steeringBehaviors)
		{
			SteeringOutput steeringOutput = steerBehavior.GetSteering();
			if (steeringOutput.overrideUpdateReset)
			{
				SteeringOutput steeringOutput2 = this.steering;
				steeringOutput2.linear.x = steeringOutput2.linear.x + steeringOutput.linear.x;
				SteeringOutput steeringOutput3 = this.steering;
				steeringOutput3.linear.z = steeringOutput3.linear.z + steeringOutput.linear.z;
				this.steering.angular = 0f;
				this.overrideDirection = true;
				this.overrideVector = this.steering.linear;
				break;
			}
			float weight = steerBehavior.Weight;
			SteeringOutput steeringOutput4 = this.steering;
			steeringOutput4.linear.x = steeringOutput4.linear.x + steeringOutput.linear.x * weight;
			SteeringOutput steeringOutput5 = this.steering;
			steeringOutput5.linear.z = steeringOutput5.linear.z + steeringOutput.linear.z * weight;
			this.steering.angular += steeringOutput.angular * weight;
			this.overrideDirection = false;
		}
		this.velocity.x = this.velocity.x + this.steering.linear.x * deltaTime;
		this.velocity.z = this.velocity.z + this.steering.linear.z * deltaTime;
		if (this.velocity.x * this.velocity.x + this.velocity.z * this.velocity.z > num * num)
		{
			this.velocity.Normalize();
			this.velocity.x = this.velocity.x * num;
			this.velocity.z = this.velocity.z * num;
		}
		if (this.steering.linear.x == 0f && this.steering.linear.z == 0f)
		{
			this.velocity.x = 0f;
			this.velocity.y = 0f;
			this.velocity.z = 0f;
		}
		this.rotation += this.steering.angular * deltaTime;
		if (this.steering.angular == 0f)
		{
			this.rotation = 0f;
		}
		this.direction = this.myTransform.InverseTransformDirection(this.velocity);
		if (this.anim != null)
		{
			if (num > 0f)
			{
				this.anim.SetWalkSpeed(this.direction, this.direction.z / (num * this.currentSpeedDivider));
			}
			else
			{
				this.anim.SetWalkSpeed(this.direction, 0f);
			}
		}
	}

	// Token: 0x060009DB RID: 2523 RVA: 0x00059174 File Offset: 0x00057374
	private void ResetMovementVariables()
	{
		this.velocity = Vector3.zero;
		this.rotation = 0f;
	}

	// Token: 0x060009DC RID: 2524 RVA: 0x0005918C File Offset: 0x0005738C
	public void SetSteering(SteeringOutput steering)
	{
		this.steering = steering;
	}

	// Token: 0x060009DD RID: 2525 RVA: 0x00059198 File Offset: 0x00057398
	public void StartSeekBehavior(IKinematic target, float separation, float duration)
	{
		this.ResetMovementVariables();
		SteerBehavior item = new Seek(this, target, separation, duration);
		this.steeringBehaviors.Add(item);
		this.AddSharedBehaviors();
		this.move = true;
	}

	// Token: 0x060009DE RID: 2526 RVA: 0x000591D0 File Offset: 0x000573D0
	public void StartFace(IKinematic target)
	{
		this.ResetMovementVariables();
		SteerBehavior item = new Face(this, target);
		this.steeringBehaviors.Add(item);
		this.move = true;
	}

	// Token: 0x060009DF RID: 2527 RVA: 0x00059200 File Offset: 0x00057400
	public void StartFlee(IKinematic target)
	{
		this.ResetMovementVariables();
		SteerBehavior item = new Flee(this, target);
		this.steeringBehaviors.Add(item);
		if (this.wander == null)
		{
			this.wander = new Wander(this);
		}
		this.wander.Weight = 0.6f;
		this.steeringBehaviors.Add(this.wander);
		this.AddSharedBehaviors();
		this.move = true;
	}

	// Token: 0x060009E0 RID: 2528 RVA: 0x0005926C File Offset: 0x0005746C
	public void Stop()
	{
		if (this.anim != null)
		{
			this.anim.UpdateAnimationSpeed();
		}
		this.steeringBehaviors = new List<SteerBehavior>();
		this.move = false;
		this.stop = false;
	}

	// Token: 0x060009E1 RID: 2529 RVA: 0x000592A4 File Offset: 0x000574A4
	public void StartPursueBehavior(IKinematic target)
	{
		this.ResetMovementVariables();
		SteerBehavior item = new Pursue(this, target);
		this.steeringBehaviors.Add(item);
		this.AddSharedBehaviors();
		this.move = true;
	}

	// Token: 0x060009E2 RID: 2530 RVA: 0x000592D8 File Offset: 0x000574D8
	public void StartWanderBehavior()
	{
		this.ResetMovementVariables();
		if (this.wander == null)
		{
			this.wander = new Wander(this);
		}
		this.wander.Weight = 1f;
		this.steeringBehaviors.Add(this.wander);
		this.AddSharedBehaviors();
		this.move = true;
	}

	// Token: 0x060009E3 RID: 2531 RVA: 0x00059330 File Offset: 0x00057530
	public SteerBehavior StartArriveBehavior(IKinematic target)
	{
		this.ResetMovementVariables();
		SteerBehavior steerBehavior = new WaypointArrive(this, target);
		this.steeringBehaviors.Add(steerBehavior);
		this.AddSharedBehaviors();
		this.move = true;
		return steerBehavior;
	}

	// Token: 0x060009E4 RID: 2532 RVA: 0x00059368 File Offset: 0x00057568
	public SteerBehavior StartMoveInDirectionBehavior(VectorKinematic target)
	{
		this.ResetMovementVariables();
		SteerBehavior steerBehavior = new MoveInDirection(this, target);
		this.steeringBehaviors.Add(steerBehavior);
		this.AddSharedBehaviors();
		this.move = true;
		return steerBehavior;
	}

	// Token: 0x060009E5 RID: 2533 RVA: 0x000593A0 File Offset: 0x000575A0
	private void AddSharedBehaviors()
	{
		this.ResetMovementVariables();
		if (this.look == null)
		{
			this.look = new LookWhereYouAreGoing(this);
		}
		this.steeringBehaviors.Add(this.look);
		if (this.avoidWall == null)
		{
			this.avoidWall = new AvoidWall(this, this.obstacleDetector);
		}
		this.steeringBehaviors.Add(this.avoidWall);
	}

	// Token: 0x060009E6 RID: 2534 RVA: 0x0005940C File Offset: 0x0005760C
	public void StartCustomBehavior()
	{
		this.ResetMovementVariables();
		this.walkController.Initialize(this);
		this.steeringBehaviors.Add(this.walkController.customSteer);
		if (this.look == null)
		{
			this.look = new LookWhereYouAreGoing(this);
		}
		this.steeringBehaviors.Add(this.look);
		this.move = true;
	}

	// Token: 0x04000F51 RID: 3921
	public EntityBase entity;

	// Token: 0x04000F52 RID: 3922
	public float tileWidth = 0.1f;

	// Token: 0x04000F53 RID: 3923
	public float speedModifier = 1f;

	// Token: 0x04000F54 RID: 3924
	public float currentSpeedDivider = 1f;

	// Token: 0x04000F55 RID: 3925
	public float maxSpeedBase = 0.8f;

	// Token: 0x04000F56 RID: 3926
	public float maxAccel = 0.5f;

	// Token: 0x04000F57 RID: 3927
	public float maxRotation = 100f;

	// Token: 0x04000F58 RID: 3928
	public float maxAngularAccel = 1000f;

	// Token: 0x04000F59 RID: 3929
	public float rotation;

	// Token: 0x04000F5A RID: 3930
	public Vector3 velocity;

	// Token: 0x04000F5B RID: 3931
	protected SteeringOutput steering;

	// Token: 0x04000F5C RID: 3932
	public bool move;

	// Token: 0x04000F5D RID: 3933
	private List<SteerBehavior> steeringBehaviors;

	// Token: 0x04000F5E RID: 3934
	private ObstacleDetector obstacleDetector;

	// Token: 0x04000F5F RID: 3935
	public AnimationManager anim;

	// Token: 0x04000F60 RID: 3936
	private bool stop;

	// Token: 0x04000F61 RID: 3937
	private Transform myTransform;

	// Token: 0x04000F62 RID: 3938
	private int behaviorCount;

	// Token: 0x04000F63 RID: 3939
	private bool overrideDirection;

	// Token: 0x04000F64 RID: 3940
	private Vector3 overrideVector;

	// Token: 0x04000F65 RID: 3941
	private SteerBehavior look;

	// Token: 0x04000F66 RID: 3942
	private SteerBehavior avoidWall;

	// Token: 0x04000F67 RID: 3943
	private SteerBehavior wander;

	// Token: 0x04000F68 RID: 3944
	private Vector3 forward;

	// Token: 0x04000F69 RID: 3945
	private Vector3 direction;

	// Token: 0x04000F6A RID: 3946
	private Rigidbody rbody;

	// Token: 0x04000F6B RID: 3947
	private Vector3 upVector;

	// Token: 0x04000F6C RID: 3948
	public WalkController walkController;
}
